package gew.utils;

import java.util.Arrays;
import java.util.List;

public class SqlUtils {

    public static final String getDoubleQuotedListFromHyphenList(String source) {
        return QUOTE_DBL
                .concat(source.replace(HYPHEN, QUOTE_DBL.concat(COMMA).concat(QUOTE_DBL)))
                .concat(QUOTE_DBL);
    }
    public static final char CHAR_COMMA = ',';
    public static final char CHAR_POINT = '.';
    public static final char CHAR_SPACE = ' ';

    public static final String TOTAL = "TOTAL";
    public static final String SQL_FORMAT = "FORMAT";
    public static final String SQL_REPLACE = "replace";
    public static final String EMPTY = "";
    public static final String AT = "@";
    public static final String SEMICOLON = ";";
    public static final String HYPHEN = "-";
    public static final String AMPERSAND = "&";
    public static final String BRACKET_SQUARE_LEFT = "[";
    public static final String BRACKET_SQUARE_RIGHT = "]";
    public static final String BRACKET_FIGURE_LEFT = "{";
    public static final String BRACKET_FIGURE_RIGHT = "}";
    public static final String SQL_SELECT = "SELECT";
    public static final String SQL_GROUP_CONCAT = "GROUP_CONCAT";
    public static final String SQL_IF = "IF";
    public static final String SQL_IN = "IN";
    public static final String SQL_NOT_IN = "NOT IN";
    public static final String EQUALS = "=";
    public static final String SQL_NULL = "NULL";
    public static final String SQL_WHERE = "WHERE";
    public static final String SQL_DESC = "DESC";
    public static final String SQL_AND = "AND";
    public static final String SQL_DISTINCT = "DISTINCT";
    public static final String SPACE = " ";
    public static final String SQL_IS_NOT_NULL = "IS NOT NULL";
    public static final String ASTERISK = "*";
    public static final String COLON = ":";
    public static final String POINT = ".";
    public static final String PLUS = "+";
    public static final String NEW_LINE = "\n";
    public static final String SQL_FROM = "FROM";
    public static final String BRACKET_LEFT = "(";
    public static final String BRACKET_RIGHT = ")";
    public static final String SQL_UNION = "UNION";
    public static final String SQL_UNION_ALL = "UNION ALL";
    public static final String SQL_ALL = "ALL";
    public static final String SQL_ORDER_BY = "ORDER BY";
    public static final String SQL_GROUP_BY = "GROUP BY";
    public static final String QUOTE = "'";
    public static final String SQL_SEPARATOR = "SEPARATOR";
    public static final String QUOTE_DBL = "\"";
    public static final String r = "r";
    public static final String COMMA = ",";
    public static final String SQL_ALIAS_OB = "ob";
    public static final String SQL_ALIAS_D = "d";
    public static final String SQL_ALIAS_F = "f";
    public static final String SQL_ALIAS_ID = "id";
    public static final String SQL_C_ROW_PREFIX = "r";
    public static final String SQL_C_DATA_POSTFIX = "_data";
    public static final String SQL_C_DISP_POSTFIX = "_disp";
    public static final String SQL_C_DESC_POSTFIX = "_desc";
    public static final String SQL_ALIAS_C_GROUP = "c_group";
    public static final String SQL_ALIAS_C_DATA = "c_data";
    public static final String SQL_ALIAS_C_DISP = "c_disp";
    public static final String SQL_ALIAS_C_DESC = "c_desc";
    public static final String SQL_ALIAS_SQL_DATA = "sql_data";
    public static final String SQL_ALIAS_SQL_DISP = "sql_disp";
    public static final String SQL_ALIAS_SQL_DESC = "sql_desc";
    public static final String SQL_C_GROUP_ROW = "1";
    public static final String SQL_C_GROUP_COL = "2";
    public static final String SQL_ALWAYS_TRUE = "1=1";
    public static final String SQL_CONCAT = "CONCAT";
    public static final String SQL_COUNT = "COUNT";
    public static final String SQL_LIMIT = "LIMIT";
    public static final String SQL_LEFT = "LEFT";
    public static final String SQL_LEFT_JOIN = "LEFT JOIN";
    public static final String SQL_JOIN = "JOIN";
    public static final String SQL_ON = "ON";
    public static final String ZERO = "0";
    public static final String ONE = "1";
    public static final String TWO = "2";
    public static final String SLASH = "/";
    public static final String OCTOTHORPE = "#";
    public static final String QUESTION = "?";
    public static final String SQL_CASE = "CASE";
    public static final String SQL_WHEN = "WHEN";
    public static final String SQL_THEN = "THEN";
    public static final String SQL_ELSE = "ELSE";
    public static final String SQL_END = "END";
    public static final String SQL_IS_NULL = "ISNULL";
    public static final String HTML_BR = "<br/>";
    public static final String HTML_B_OPEN = "<b>";
    public static final String HTML_B_CLOSE = "</b>";
    public static final String HTTP = "http://";
    public static final String UNDERBAR = "_";
    public static final String VERTICAL_BAR = "|";
    public static final String X = "x";

    public static String concatString(List<String> s, String separator) {
        String[] ar = new String[s.size()];
        s.toArray(ar);
        return concatString(ar, separator);
    }

    public static String concatString(String[] s, String separator) {
        StringBuilder buf = new StringBuilder();
        String prev = "";
        for (Integer i = 0; i < s.length; i++) {
            if (!s[i].isEmpty()) {
                if (!prev.isEmpty()) {
                    buf.append(separator);
                }
                buf.append(s[i]);
                prev = s[i];
            }
        }
        return buf.toString();
    }

    public static final String excludeUrlCode(String urlCode, String url) {
        String[] codes = url.split(SqlUtils.HYPHEN);
        String r = "";
        for (int i = 0; i < codes.length; i++) {
            if (!codes[i].equals(urlCode)) {
                r = r.concat(SqlUtils.HYPHEN).concat(codes[i]);
            }
        }
        return r.isEmpty() ? r : r.substring(1);
    }

    public static final String includeUrlCode(String urlCode, String url) {
        if (url.isEmpty()) {
            return urlCode;
        } else {
            String[] codes = url.concat(SqlUtils.HYPHEN).concat(urlCode).split(SqlUtils.HYPHEN);
            Arrays.sort(codes);
            return String.join(SqlUtils.HYPHEN, codes);
        }
    }
}
